package com.atmilan.quartz.sync.pull.europe.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atmilan.quartz.model.entity.europe.EuropeCategory;
import com.atmilan.quartz.sync.pull.europe.mapper.EuropeCategoryMapper;
import com.atmilan.quartz.sync.pull.europe.service.IEuropeCategoryService;
import com.atmilan.quartz.utils.EuropeSendXmlUtil;
import com.atmilan.quartz.utils.EuropeUtil;
import com.atmilan.quartz.utils.STAXUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.date.DateUtil;

@Service
@Transactional
public class EuropeCategoryServiceImpl extends ServiceImpl<EuropeCategoryMapper, EuropeCategory> implements IEuropeCategoryService {

    @Value("${europe.url}")
    private String url;

    @Value("${europe.username}")
    private String username;

    @Value("${europe.password}")
    private String password;

    @Autowired
    IEuropeCategoryService europeCategoryService;

    @Override
    public Boolean getCategorys() throws IOException {
        String sendMsg = "<tem:GetGoodsCategory>" + "</tem:GetGoodsCategory>";
        String sendXML = EuropeSendXmlUtil.getSendMsg(sendMsg);
        String respXml = EuropeUtil.getEuropeXml(username, password, url, sendXML);
        //解析xml文档
        List<EuropeCategory> categoryList = new ArrayList<>();
        try {
            List<Map<String, Object>> europMapList = STAXUtil.getEuropMapList(respXml, "GetGoodsCategory");
            for(Map<String,Object> map : europMapList){
                EuropeCategory europeCategory = new EuropeCategory();
                europeCategory.setCategoryId(Integer.parseInt((String)map.get("ID")));
                europeCategory.setCategoryName((String)map.get("Name"));
                europeCategory.setParentId(Integer.parseInt((String)map.get("ParentID")));
                europeCategory.setParentName((String)map.get("ParentName"));
                europeCategory.setGoodsType((String)map.get("GoodsType"));
                europeCategory.setCreatedTime(DateUtil.date());
                europeCategoryService.save(europeCategory);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
